﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace AnimatedSprites
{
    class MenuManager : AbstractManager
    {
        protected int currentOption;
        protected int optionNumber;
        //public Game1 game1;
        List<String> buttonnameList = new List<String>() ;
        List<Texture2D> buttonList = new List<Texture2D>() ;
        Texture2D background;
        Texture2D button_select;
        Vector2 select_pos ;
        Vector2 cur_pos;
        bool press_u = false;
        bool press_d = false;
        protected bool press_e = false;
        
        public MenuManager(Game1 game)
        : base(game)
        {
            game1 = game;
            currentOption = 0;
                // TODO: Construct any child components here
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            //在这里定义一些初始化的内容，选项的精灵
            background = Game.Content.Load<Texture2D>("Images//background");                                        //////
            button_select = Game.Content.Load<Texture2D>("Images//button_select");
            //将list中所有的sprite，load进content 
            for(int i = 0; i < buttonnameList.Count; i++)
                buttonList.Add(Game.Content.Load<Texture2D>(buttonnameList[i])) ;
            base.LoadContent();
        }

            public override void Update(GameTime gameTime)
            {
                //检测用户上下按钮输入，更新currentOption的值,如果检测到enter，通过game1掉用其中的currentGameState；
                KeyboardState keyboardState = Keyboard.GetState( );
                if (keyboardState.IsKeyDown(Keys.Down))
                {
                    if(currentOption<optionNumber-1 && press_d == false){
                        currentOption++;
                        select_pos.Y += 90;
                    }
                    press_d = true;
                }
                if (keyboardState.IsKeyUp(Keys.Down))
                    press_d = false;
                if(keyboardState.IsKeyDown(Keys.Up))
                {
                    if (currentOption > 0 && press_u == false){
                        currentOption--;
                        select_pos.Y -= 90;
                    }
                    press_u = true;
                }
                if (keyboardState.IsKeyUp(Keys.Up))
                    press_u = false;
            }

            public override void Draw(GameTime gameTime)
            {
                //用for循环配合currentOption的值调用并显示spriteList里的精灵
                cur_pos = new Vector2(335, 310);
                select_pos = new Vector2(335, 310);
                select_pos.Y += currentOption * 90;
                spriteBatch.Begin();
                //background
                spriteBatch.Draw(background, Vector2.Zero, Color.White);    // 背景
                spriteBatch.Draw(button_select, select_pos, Color.White);   // 选择底板
                for (int i = 0; i < buttonList.Count; i++, cur_pos.Y += 90)
                {   // 如果在结束游戏状态，需要隐藏其中一个选项
                    if (i == game1.hide && ( game1.currentGameState == Game1.GameState.GameOver ||game1.currentGameState == Game1.GameState.GameEnd) ) i++;
                        spriteBatch.Draw(buttonList[i], cur_pos, Color.White);
                }
                spriteBatch.End();
                base.Draw(gameTime);
            }
            //这里感觉用sprite太浪费
            //public void addSprite(Sprite newSprite){
            //    //将newSprite添加到buttonList中
            //    buttonList.Add(newSprite);
            //}
            public void addButtonname(String newButton)
            {
                //将newButton添加到buttonList中
                buttonnameList.Add(newButton);
            }

       }
}
